#coding=utf-8
#user dir

USER_DIR=$(echo ~)
echo user dir is: $USER_DIR
install_dir=$USER_DIR/juke/rubClassify_pkg
if [ ! -d "$install_dir" ]; then
    echo "install dir not exist"
    exit 1
else
    echo "install dir exist"
fi
echo =======================install dir:$install_dir===========================
cd $install_dir
sudo apt-get install python3-pip -y
pip3 install pip-21.3.1-py3-none-any.whl
pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple/

echo ===========pip3 ============
sudo pip3 install ipython==7.16.3 --trusted-host mirrors.aliyun.com
pip3 install psutil==5.9.6 --trusted-host mirrors.aliyun.com


echo ================dobot rules=================
file=/etc/udev/rules.d/70-ttyusb.rules
if [ ! -f "$file" ]; then
    # *KERNEL=="ttyUSB[0-99]*", MODE="0666"
    echo >>"*KERNEL==\"ttyUSB[0-99]*\", MODE=\"0666\"">>$file
    echo >>"*KERNEL==\"ttyCH343USB[0-99]*\", MODE=\"0666\"">>$file
    # echo "SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"0483\", ATTRS{idProduct}==\"5740\", MODE=\"0666\", SYMLINK+=\"dobot\"">>$file
fi

# =======2. apt======
echo 2.备份之前的源
echo ========================更新软件=====================================
if [ -f "/etc/apt/sources.list_juke.bak" ]; then
    echo "备份文件已存在"
else
    echo "备份文件不存在"
    sudo cp /etc/apt/sources.list /etc/apt/sources.list_juke.bak
    echo 更换源
    sudo cp arm_source.list /etc/apt/sources.list
    sudo apt-get update -y
fi


echo ========================apt dev=====================================
sudo apt-get install git cmake python3-dev -y
sudo apt-get install libhdf5-serial-dev hdf5-tools libhdf5-dev zlib1g-dev zip libjpeg8-dev -y
echo 3.安装pytorch先安装依赖：
sudo apt-get install libopenblas-base libopenmpi-dev -y
sudo apt-get install python3-pyaudio -y

echo  ========================3.camera=====================================

gxipy=/usr/local/lib/python3.6/dist-packages/gxipy-2.0.2103.9041-py3.6.egg
if [ -d "$gxipy" ]; then
    tar -zxvf Galaxy_Linux-armhf_Gige-U3_32bits-64bits_1.3.2104.9022.tar.gz
    cd Galaxy_Linux-armhf_Gige-U3_32bits-64bits_1.3.2104.9022/
    bash ./Galaxy_camera.run

    cd ..
    tar -zxvf Galaxy_Linux_Python_2.0.2103.9101.tar.gz
    cd Galaxy_Linux_Python_2.0.2103.9101/api/
    python3 setup.py build
    sudo python3 setup.py install
    cd ..
    cd ..

    sudo rm -rf Galaxy_Linux-armhf_Gige-U3_32bits-64bits_1.3.2104.9022/
    sudo rm -rf Galaxy_Linux_Python_2.0.2103.9101/
else
    echo "gxipy already installed"
fi

echo ==========================labelImg=================================
if [ ! -f "/usr/local/bin/labelImg" ]; then
    echo "install labelImg..."
    sudo apt-get install pyqt5-dev-tools -y
    if [ ! -d "labelImg" ]; then
        unzip labelImg.zip
    fi
    cd labelImg
    current_dir=$(pwd)
    echo "python3 ${current_dir}/labelImg.py" > labelImg
    sudo cp labelImg /usr/local/bin
    sudo chmod +x labelImg /usr/local/bin/labelImg
    cd ..
else
    echo "labelImg already installed"
fi

echo =================install code==============
if [ ! -f "/usr/bin/code" ]; then
    sudo dpkg -i code_1.81.0-1690979901_arm64.deb
else
    echo "code already installed"
fi
echo =============4.dist-packages install================
if [ ! -d "dist-packages" ]; then
    unzip dist-packages33.zip 
else
    echo "dist-packages already exists"
    sudo rm -rf dist-packages
    unzip dist-packages33.zip 
fi
sudo cp -r ./dist-packages /usr/local/lib/python3.6
sudo rm -rf dist-packages

echo  =============5.torchvision==0.9.0================
pwd
if [ ! -d "torchvision0.9.0" ]; then
    unzip torchvision0.9.0.zip
fi
cd torchvision0.9.0
export BUILD_VERSION=0.9.0
sudo python3 setup.py install --user
cd ..
sudo rm -rf torchvision0.9.0

echo =======================安装完成=====================================
echo =======================last dir:=====================================
pwd
